<template>
  <div>
    <div class="botton-top-contrainer">
      <a-space>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"
          v-if="hasPermission('business:transferStorage:add')">
          新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"
          v-if="hasPermission('business:transferStorage:exportXls')">
          导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </a-space>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">

      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="busMiscellaneousStorage-busMiscellaneousStorage" setup>

import { reactive, toRaw } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { useRouter } from 'vue-router'
import { columns, searchFormSchema } from './data/columns';
import { list, batchDelete, getImportUrl, getExportUrl} from './api';

import { usePermission } from '/@/hooks/web/usePermission';

const router = useRouter()


const { hasPermission } = usePermission();

//注册table数据
const { tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 120,
      fixed: 'right',
    },
  },
  exportConfig: {
    name: '',
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});

const [registerTable, { reload, setProps }, { rowSelection, selectedRowKeys }] = tableContext;

function handleAdd() {
  router.push({
    name: 'plan-scheduling-feol-resource-operation',
    query: { title: '新增' }
  })
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  router.push({
    name: 'plan-scheduling-feol-resource-operation',
    query: { id: record.id, title: '编辑' }
  })
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'business:transferStorage:edit', //通过权限指令控制显示（有权限显示/无权限不显示）
    }
  ];
}

/**
 * 下拉操作栏
 */

// 高级查询配置
const superQueryConfig = reactive({
  billTypeName: { title: '单据类型', view: 'text', type: 'string', order: 1 },
  billCode: { title: '单据编号', view: 'text', type: 'string', order: 1 },
  shipmentOrganizationName: { title: '入库组织名称', view: 'text', type: 'string', order: 1 },
  shipmentWarehouseName: { title: '仓库名称', view: 'date', type: 'string', order: 1 },
  sourceCategoryName: { title: '来源类别名称', view: 'text', type: 'string', order: 1 },
  srcBillCode: { title: '来源单据编号', view: 'text', type: 'string', order: 1 },
  businessDate: { title: '业务日期', view: 'date', type: 'string', order: 1 },
  remark: { title: '备注', view: 'text', type: 'string', order: 1 },
});
//执行查询
function handleSuperQuery(params) {
  setProps({ searchInfo: toRaw(params) });
  reload();
}
</script>

<style scoped>
.botton-top-contrainer {
  padding: 10px 10px 0px 10px;
}
</style>
